<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>生产管理--工序物料设计--制定工序物料设计单</title>
</head>
<!-- 新 Bootstrap4 核心 CSS 文件 -->
<link rel="stylesheet" href="https://cdn.staticfile.org/twitter-bootstrap/4.3.1/css/bootstrap.min.css">


<link rel="stylesheet" href="../../css/bootstrap-table.min.css">

<!-- jQuery文件。务必在bootstrap.min.js 之前引入 -->
<script src="https://cdn.staticfile.org/jquery/3.2.1/jquery.min.js"></script>

<!-- bootstrap.bundle.min.js 用于弹窗、提示、下拉菜单，包含了 popper.min.js -->
<script src="https://cdn.staticfile.org/popper.js/1.15.0/umd/popper.min.js"></script>

<!-- 最新的 Bootstrap4 核心 JavaScript 文件 -->
<script src="https://cdn.staticfile.org/twitter-bootstrap/4.3.1/js/bootstrap.min.js"></script>



<script src="../../js/bootstrap-table.min.js"></script>

<script src="../../js/bootstrap-table-zh-CN.min.js"></script>
<body>

<div>
    <table id="mater_tab">

    </table>
</div>
<!--制定-->
<div class="modal fade" id="shejiwuliaoshejimo">
    <div class="modal-dialog modal-xl">
        <div class="modal-content">

            <!-- 模态框头部 -->
            <div class="modal-header">
                <h6 class="modal-title">您正在做的业务是：生产管理--工序物料设计--制定工序物料设计单</h6>
                <button type="button" class="close" data-dismiss="modal">&times;</button>
            </div>

            <!-- 模态框主体 -->
            <div class="modal-body" style="background-color: darkgray;">
                <div style="display: flex;justify-content: flex-end">
                    <button type="button" class="btn btn-info" id="shjiwctijiaobutton">提交</button>
                    <button type="button" class="btn btn-secondary"style="margin-left: 5px;" data-dismiss="modal">返回</button>
                </div>

                <div style="border: 1px solid black;margin-top: 10px; background-color: white;display: flex;flex-direction: column">
                    <h5 style="text-align: center;margin-top: 20px">工序物料设计单</h5>
                    <div style="display: flex;">
                        <div  style="margin-left: 10%;width: 40%;" >工序单编号：<span id="query_designId"></span></div>
                        <div style="width: 40%;display: flex"><div style="width: 23%">设计人：</div><span id="query_designer"></span></div>
                    </div>
                    <div  style="display: flex;margin-top: 5px;">
                        <div  style="margin-left: 10%;width: 30%;" >产品名称：<span id="query_productName"></span></div>
                        <div  style="margin-left: 10%;width: 40%;" >产品编号：<span id="query_productId"></span></div>
                    </div>
                    <div style="display: flex;">
                        <table class="table-sm  table-bordered table-hover" id="shengcahngtab" style="margin-left: 10%;margin-top: 5px;text-align: center"  cellpadding="0" cellspacing="0" width="80%;">
                            <tr class="table-primary" style="color: white">
                                <td>工序序号</td>
                                <td>工序名称</td>
                                <td>工序编号</td>
                                <td>描述</td>
                                <td>工时数</td>
                                <td>工时单位</td>
                                <td>工时成本</td>
                                <td>工时成本小计（元）</td>
                                <td>设计</td>
                            </tr>
                        </table>
                    </div>
                    <div>
                        <div  style="display: flex;margin-top: 5px;" >
                            <div  style="margin-left: 10%;width: 30%;" >工时总成本：<span id="query_costPriceSum"></span></div>
                            <div  style="margin-left: 10%;width: 30%;" >物料总成本：<span id="sheng_moduleCostPriceSum"></span></div>
                        </div>
                        <div  style="display: flex;margin-top: 5px;">
                            <div  style="margin-left: 10%;width: 30%;" >审核人：<span id="query_checker"></span></div>
                            <div  style="margin-left: 10%;width: 40%;" >审核时间：<span id="query_checkTime"></span></div>
                        </div>
                        <div style="display: flex;margin-bottom: 30px;">
                            <div style="margin-left:10%;margin-top: 5px;display: flex;width: 100%">
                                设计要求：
                                <span id="query_procedureDescribe"></span>
                            </div>
                            <input type="hidden" id="shejid_id">
                            <input type="hidden" id="wuliaozucheng_id">
                        </div>
                    </div>

                </div>
            </div>
        </div>
    </div>
</div>

<div class="modal fade" id="shejimtk">
    <div class="modal-dialog modal-xl">
        <div class="modal-content">

            <!-- 模态框头部 -->
            <div class="modal-header">
                <h6 class="modal-title">您正在做的业务是：生产管理--工序物料设计--制定工序物料设计单</h6>
                <button type="button" class="close" data-dismiss="modal">&times;</button>
            </div>

            <!-- 模态框主体 -->
            <div class="modal-body" style="background-color: white;height:450px;">
                <div style="display: flex;justify-content: flex-end">
                    <button type="button" class="btn btn-primary" onclick="tijiao()">提交</button>
                    <button type="button" class="btn btn-secondary"style="margin-left: 5px;" data-dismiss="modal">返回</button>
                </div>

                <div style="display: flex;margin-left: 5%;margin-top: 2%;flex-direction: column">
                    <h5 style="text-align: center;margin-top: 20px">工序物料设计单</h5>
                    <table class="table table-striped table-bordered table-hover" id="sheji_table"  cellspacing="0" cellpadding="0" style="width:97%;text-align: center;">
                        <thead>
                        <tr>
                            <td>序号</td>
                            <td>物料名称</td>
                            <td>物料编号</td>
                            <td>设计数量</td>
                            <td>可用数量</td>
                            <td>单位</td>
                            <td>单价</td>
                            <td>工序本数量</td>
                        </tr>
                        </thead>
                        <tbody id="sheji_tbody">

                        </tbody>
                    </table>
                    <input id="gx_parentId" type="hidden">
                </div>
            </div>
        </div>
    </div>
</div>

<script>
    $(function () {
        $("#mater_tab").bootstrapTable({
            method: "post",
            contentType: "application/x-www-form-urlencoded;charset=utf-8",
            url: "/lc/querymaterialldata",
            striped: true,
            pageNumber: 1,
            pagination: true,
            sidePagination: "server",
            pageSize: 5,
            pageList: [5, 8, 10],
            queryParams: function (parms) {
                return {
                    pageNumber: (parms.offset / parms.limit) + 1,
                    pageSize: parms.limit,
                }
            },
            columns: [
                {title: "工序设计单编号", field: "designId", align: "center"},
                {title: "产品编号", field: "productId", align: "center"},
                {title: "产品名称", field: "productName", align: "center"},
                {title: "设计人", field: "designer", align: "center"},
                {title: "登记时间", field: "registerTime", align: "center"},
                {title: "工时总成本", field: "costPriceSum", align: "center"},
                {title: "制定", field: "checkTag", align: "center",
                    formatter: function (value, row, index) {
                        var btn1 = "<button  class=\"btn btn-primary btn-sm\" data-toggle=\"modal\" onclick='zhidingwuliaosheji("+JSON.stringify(row)+")' data-target=\"#shejiwuliaoshejimo\">制定设计单</button>";
                        return btn1;
                    }
                },
            ],
        })
    })
    function  zhidingwuliaosheji(zz) {
        $("#shejid_id").val(zz.id)
        $("#query_designId").text(zz.designId);
        $("#query_designer").text(zz.designer);
        $("#query_productName").text(zz.productName);
        $("#query_productId").text(zz.productId);
        $("#query_costPriceSum").text(zz.costPriceSum);
        $("#query_checker").text(zz.checker);
        var myDate = new Date;
        $("#query_checkTime").text(myDate.toLocaleString());
        $("#query_procedureDescribe").text(zz.procedureDescribe);
        $(".sheng_tr").remove();

        $.ajax({
            type:"POST",
            data:"id="+zz.id,
            url:"/lc/queryByidcheckmingxi",
            success:function (iddata) {
                $.each(iddata,function (index,d) {
                    //查询是否当前工序是否已经进行物料组成设计，设计了就要重新设计，没设计就选择选择设计
                    $.ajax({
                        type:"POST",
                        url:"/lc/queryBymoduletrue",
                        data:"id="+d.id,
                        success:function (v) {
                            $("#gx_parentId").val(d.id)
                            if(v==null || v==''){
                                $("#shengcahngtab").append("<tr class='sheng_tr'>" +
                                    "<td>"+d.detailsNumber+"</td>" +
                                    "<td>"+d.procedureName+"</td>" +
                                    "<td>"+d.procedureId+"</td>" +
                                    "<td></td>" +
                                    "<td>"+d.labourHourAmount+"</td>" +
                                    "<td>"+d.amountUnit+"</td>" +
                                    "<td>"+d.costPrice+"</td>" +
                                    "<td>"+d.subtotal+"</td>" +
                                    "<td><a href='#' onclick='sheji("+zz.productId+","+d.id+")'>设计</a></td>" +
                                    "</tr>")
                            }else{
                                $("#shengcahngtab").append("<tr class='sheng_tr'>" +
                                    "<td>"+d.detailsNumber+"</td>" +
                                    "<td>"+d.procedureName+"</td>" +
                                    "<td>"+d.procedureId+"</td>" +
                                    "<td></td>" +
                                    "<td>"+d.labourHourAmount+"</td>" +
                                    "<td>"+d.amountUnit+"</td>" +
                                    "<td>"+d.costPrice+"</td>" +
                                    "<td>"+d.subtotal+"</td>" +
                                    "<td><a href='#' onclick='cxsheji("+d.id+")'>重新设计</a></td>" +
                                    "</tr>")
                            }

                        }
                    })
                })
            }
        })
        $.ajax({
            type:"POST",
            data:"id="+$("#query_productId").text(),
            url:"/lc/shejichax",
            success:function(zzz){
                $("#wuliaozucheng_id").val(zzz.id)
            }
        })
    }
    //设计

    function sheji(z,x) {
        $("#gx_parentId").val(x)
        $(".sheji_tr").remove()
        //查询物料组成通过的才能设计
        $.ajax({
            type:"POST",
            data:"id="+z,
            url:"/lc/shejichax",
            success:function (x) {
                $("#wuliaozucheng_id").val(x.id)
                if(x==null || x==''){
                    alert("物料组成设计尚未完成，不能进行工序物料设计，请返回。")
                }else{
                    //物料组成设计完成根据物料组成id查询物料组成明细
                    $("#sheji_tbody td").remove()
                    $.ajax({
                        type:"POST",
                        data:"id="+x.id,
                        url:"/lc/querywuliaocuchen",
                        success:function (b) {
                            $.each(b,function (index,w) {
                                $("#sheji_tbody").append("<tr class='sheji_tr'>" +
                                    "<td>"+w.detailsNumber+"</td>" +
                                    "<td>"+w.productName+"</td>" +
                                    "<td>"+w.productId+"</td>" +
                                    "<td>"+w.amount+"</td>" +
                                    "<td>"+w.residualAmount+"</td>" +
                                    "<td>"+w.amountUnit+"</td>" +
                                    "<td>"+w.costPrice+"</td>" +
                                    "<td><input type='number' style='border: none;outline: none;background: rgba(253,250,250,0.89);'></td>" +
                                    "</tr>")
                            })
                            $("#shejimtk").modal("show")
                        }
                    })
                }

            }
        })
    }
    //设计点击提交：判断可可用数量是否大于选择的数量，大于就不让提交，否则就提交设计
    function tijiao() {
        var shuz=[];
        var wulmx=[];
        var xxz=0
        var wusum=0;
        $(".sheji_tr").each(function () {
            if(parseInt($(this).find(" td:eq(4)").text())<parseInt($(this).find(" td:eq(7)>input").val()) || $(this).find(" td:eq(7)>input").val()==''){
                xxz+=1;
            }else{
                wusum+=parseInt($(this).find(" td:eq(6)").text()*$(this).find(" td:eq(7)>input").val())
                var moduleobj={
                    parentId:$("#gx_parentId").val(),
                    productId:$(this).find(" td:eq(2)").text(),
                    subtotal:parseInt($(this).find(" td:eq(6)").text()*$(this).find(" td:eq(7)>input").val()),
                    productName:$(this).find(" td:eq(1)").text(),
                    amountUnit:$(this).find(" td:eq(5)").text(),
                    costPrice:parseInt($(this).find(" td:eq(6)").text()),
                    amount:parseInt($(this).find(" td:eq(7)>input").val())
                }
                shuz.push(moduleobj);

                var wuliaozuceobj={
                    productName:$(this).find(" td:eq(1)").text(),
                    residualAmount:parseInt($(this).find(" td:eq(4)").text()-$(this).find(" td:eq(7)>input").val())
                }
                wulmx.push(wuliaozuceobj)
            }
        })

        var product={
            id:$("#gx_parentId").val(),
            subtotal:wusum
        }
        //设计工序
        if(xxz==0){
            $.ajax({
                type:"POST",
                contentType: 'application/json',
                data:JSON.stringify({mdesignprocedureDetails:product,moduleList:shuz,details:wulmx,id:$("#wuliaozucheng_id").val()}),
                url:"/lc/shejitijiao",
                success:function (l) {
                    $("#shejimtk").modal("hide")
                    $(".sheng_tr").remove();
                    $.ajax({
                        type:"POST",
                        data:"id="+$("#shejid_id").val(),
                        url:"/lc/queryByidcheckmingxi",
                        success:function (iddata) {
                            $.each(iddata,function (index,d) {
                                //查询是否当前工序是否已经进行物料组成设计，设计了就要重新设计，没设计就选择选择设计
                                $.ajax({
                                    type:"POST",
                                    url:"/lc/queryBymoduletrue",
                                    data:"id="+d.id,
                                    success:function (v) {
                                        $("#gx_parentId").val(d.id)
                                        if(v==null || v==''){
                                            $("#shengcahngtab").append("<tr class='sheng_tr'>" +
                                                "<td>"+d.detailsNumber+"</td>" +
                                                "<td>"+d.procedureName+"</td>" +
                                                "<td>"+d.procedureId+"</td>" +
                                                "<td></td>" +
                                                "<td>"+d.labourHourAmount+"</td>" +
                                                "<td>"+d.amountUnit+"</td>" +
                                                "<td>"+d.costPrice+"</td>" +
                                                "<td>"+d.subtotal+"</td>" +
                                                "<td><a href='#' onclick='sheji($(\"#query_productId\").text(),"+d.id+")'>设计</a></td>" +
                                                "</tr>")
                                        }else{
                                            $("#shengcahngtab").append("<tr class='sheng_tr'>" +
                                                "<td>"+d.detailsNumber+"</td>" +
                                                "<td>"+d.procedureName+"</td>" +
                                                "<td>"+d.procedureId+"</td>" +
                                                "<td></td>" +
                                                "<td>"+d.labourHourAmount+"</td>" +
                                                "<td>"+d.amountUnit+"</td>" +
                                                "<td>"+d.costPrice+"</td>" +
                                                "<td>"+d.subtotal+"</td>" +
                                                "<td><a href='#' onclick='cxsheji("+d.id+")'>重新设计</a></td>" +
                                                "</tr>")
                                        }

                                    }
                                })
                            })
                        }
                    })
                    $.ajax({
                        type:"POST",
                        data:"id="+$("#query_productId").text(),
                        url:"/lc/shejichax",
                        success:function(zzz){
                            $("#wuliaozucheng_id").val(zzz.id)
                        }
                    })
                }
            })
        }else{
            alert("你没有填写工序物料工序本数量或者工序本数量大于可用数量")
        }
    }
    function  cxsheji(az) {
        // 根据父级序号查询到所有的物料设计明细单，获得工序本数量
        $.ajax({
            type: "POST",
            url: "/lc/queryBymoduletrue",
            data: "id=" +az,
            success: function (k) {
                var updateobj=[];
                var uobj={};
                $.each(k,function (index,p) {
                    uobj={
                        parentId:p.parentId,
                        productId:p.productId,
                        amount:p.amount,
                        productName:p.productName
                    }
                    updateobj.push(uobj);
                })
                $.ajax({
                    type:"POST",
                    contentType: 'application/json',
                    data:JSON.stringify({details:updateobj,id:$("#wuliaozucheng_id").val()}),
                    url:"/lc/updategxmxfalse",
                    success:function(xx){

                        $(".sheng_tr").remove();
                        $.ajax({
                            type:"POST",
                            data:"id="+$("#shejid_id").val(),
                            url:"/lc/queryByidcheckmingxi",
                            success:function (iddata) {
                                $.each(iddata,function (index,d) {
                                    //查询是否当前工序是否已经进行物料组成设计，设计了就要重新设计，没设计就选择选择设计
                                    $.ajax({
                                        type:"POST",
                                        url:"/lc/queryBymoduletrue",
                                        data:"id="+d.id,
                                        success:function (v) {
                                            $("#gx_parentId").val(d.id)
                                            if(v==null || v==''){
                                                $("#shengcahngtab").append("<tr class='sheng_tr'>" +
                                                    "<td>"+d.detailsNumber+"</td>" +
                                                    "<td>"+d.procedureName+"</td>" +
                                                    "<td>"+d.procedureId+"</td>" +
                                                    "<td></td>" +
                                                    "<td>"+d.labourHourAmount+"</td>" +
                                                    "<td>"+d.amountUnit+"</td>" +
                                                    "<td>"+d.costPrice+"</td>" +
                                                    "<td>"+d.subtotal+"</td>" +
                                                    "<td><a href='#' onclick='sheji($(\"#query_productId\").text(),"+d.id+")'>设计</a></td>" +
                                                    "</tr>")
                                            }else{
                                                $("#shengcahngtab").append("<tr class='sheng_tr'>" +
                                                    "<td>"+d.detailsNumber+"</td>" +
                                                    "<td>"+d.procedureName+"</td>" +
                                                    "<td>"+d.procedureId+"</td>" +
                                                    "<td></td>" +
                                                    "<td>"+d.labourHourAmount+"</td>" +
                                                    "<td>"+d.amountUnit+"</td>" +
                                                    "<td>"+d.costPrice+"</td>" +
                                                    "<td>"+d.subtotal+"</td>" +
                                                    "<td><a href='#' onclick='cxsheji("+d.id+")'>重新设计</a></td>" +
                                                    "</tr>")
                                            }

                                        }
                                    })
                                })
                            }
                        })
                        $.ajax({
                            type:"POST",
                            data:"id="+$("#query_productId").text(),
                            url:"/lc/shejichax",
                            success:function(zzz){
                                $("#wuliaozucheng_id").val(zzz.id)
                            }
                        })

                    }

                })

            }
        })
    }
    // 设计完成点击提交,
    $(function () {

        $("#shjiwctijiaobutton").click(function () {
            // 查询是否所有工序都是显示已设计
            $.ajax({
                type:"POST",
                data:"id="+$("#shejid_id").val(),
                url:"/lc/checkgxshifousheji",
                success:function (y) {
                    if(y<$(".sheng_tr").length){
                        alert("你尚有工序物料未完成设计，请完成后提交")
                    }else{
                        //查询工序物料明细组成表是否为0
                        $.ajax({
                            type:"post",
                            data:"id="+$("#wuliaozucheng_id").val(),
                            url:"/lc/queryamout0",
                            success:function (vv) {
                                if(vv<$(".sheji_tr").length){
                                    alert("对不起，尚有可用物料或者物料不足，请您仔细检查工序物料设计，请返回！");
                                }else{
                                    //根据产品工序设计表的编号查到每个工序明细的物料数量然后计算总数为物料总成本(parentid=id)
                                    $.ajax({
                                        type:"post",
                                        data:"id="+$("#shejid_id").val(),
                                        url:"/lc/ssmx",
                                        success:function (bn) {
                                            var wuliaozongshusum=0;
                                            $.each(bn,function (index,qw) {
                                                wuliaozongshusum+=qw.moduleSubtotal;
                                            })
                                            var peoductobj={
                                                id:$("#shejid_id").val(),
                                                moduleCostPriceSum:wuliaozongshusum,
                                            }
                                            //设计修改为已设计产品工序设计，
                                            $.ajax({
                                                type:"POST",
                                                data:JSON.stringify(peoductobj),
                                                contentType: 'application/json',
                                                url:"/lc/xiugaigxshejiyishejiwuliao",
                                                success:function () {
                                                    $("#shejiwuliaoshejimo").modal("hide");
                                                    $("#mater_tab").bootstrapTable("refresh");
                                                }
                                            })
                                        }
                                    })
                                }
                            }
                        })
                    }
                }
            })
        })
    })
</script>
</body>
</html>